
#import "WXSSleepViewController.h"
#import "WXSSleepButton.h"
#import "CLZoomPickerView.h"
#import "WXSExplainViewController.h"

static NSString *currentTime;

@interface WXSSleepViewController ()<CLZoomPickerViewDelegate, CLZoomPickerViewDataSource>

@property (nonatomic, strong) UIView * topView;
@property (nonatomic, strong) UIView * bottomView;
@property (nonatomic, strong) WXSSleepButton *button;
@property (nonatomic, strong) CLZoomPickerView *pickerView;
@property (nonatomic, strong) CLZoomPickerView *leftPickerView;
@property (nonatomic, strong) NSArray *timeArray;
@property (nonatomic, strong) NSArray *timeArr;
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UILabel *currentTimeLabel;
@property (nonatomic, strong) UILabel *surplusTimeLabel;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) NSString *hour;
@property (nonatomic, strong) NSString *min;
@property (nonatomic, assign) NSInteger H;
@property (nonatomic, assign) NSInteger M;
@property (nonatomic, assign) NSInteger Hours;
@property (nonatomic, assign) NSInteger Mins;
@property (nonatomic, strong) NSString *surTime;
@property (nonatomic, assign) BOOL isON;

@end

@implementation WXSSleepViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSubviews];
    [self setupRightBarButtonWithImageName:@"clock_on" action:@selector(turnOn)];
    [self setupLeftBarButtonWithImageName:@"explain" action:@selector(explain)];
    [self getCurrentTime];
    self.isON = YES;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(getCurrentTime) userInfo:nil repeats:YES];
    
//    NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
//    NSArray *fontNames;
//    NSInteger indFamily, indFont;
//    for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
//    {
//        NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);
//        fontNames = [[NSArray alloc] initWithArray:
//                     [UIFont fontNamesForFamilyName:
//                      [familyNames objectAtIndex:indFamily]]];
//        for (indFont=0; indFont<[fontNames count]; ++indFont)
//        {
//            NSLog(@"    Font name: %@", [fontNames objectAtIndex:indFont]);
//        }
//    }
}

- (void)setupSubviews{
 
    [self.view addSubview:self.label];

    [self.view addSubview:self.pickerView];
    [self.view addSubview:self.leftPickerView];

    _button = [WXSSleepButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(kBaseWidth/2 - 55, kBaesHeight/2 + 60, 120, 120);
    [_button setImage:[UIImage imageNamed:@"star"] forState:UIControlStateNormal];
    _button.backgroundColor = [UIColor clearColor];
    _button.layer.cornerRadius = 60;
    _button.layer.masksToBounds = YES;
    [_button addTarget:self action:@selector(star) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button];
}

#pragma mark - button action
- (void)star{
    [_button setImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
    [_pickerView removeFromSuperview];
    [_leftPickerView removeFromSuperview];
    [_label removeFromSuperview];
    [_messageLabel removeFromSuperview];
    [self.view addSubview:self.bgView];
    [_bgView addSubview:self.surplusTimeLabel];
    [_bgView addSubview:self.currentTimeLabel];
    
    if (self.isON == NO) {
        _surplusTimeLabel.text = @"闹钟未开启";
    }
    
    self.navigationItem.rightBarButtonItem.enabled = NO;
}

- (void)stop{
    [_button setImage:[UIImage imageNamed:@"star"] forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(star) forControlEvents:UIControlEventTouchUpInside];
    [_currentTimeLabel removeFromSuperview];
    [_surplusTimeLabel removeFromSuperview];
    [_bgView removeFromSuperview];
    [self.view addSubview:self.pickerView];
    [self.view addSubview:self.leftPickerView];
    [self.view addSubview:self.label];

    if (self.isON == NO) {
        [_leftPickerView removeFromSuperview];
        [_pickerView removeFromSuperview];
        [_label removeFromSuperview];
        [self.view addSubview:self.messageLabel];
    }
    
    self.navigationItem.rightBarButtonItem.enabled = YES;
}

- (void)turnOn{
    [self setupRightBarButtonWithImageName:@"clock_off" action:@selector(turnOff)];
    [_pickerView removeFromSuperview];
    [_leftPickerView removeFromSuperview];
    [_label removeFromSuperview];
    [self.view addSubview:self.messageLabel];
    self.isON = NO;
}

- (void)turnOff{
    [self setupRightBarButtonWithImageName:@"clock_on" action:@selector(turnOn)];
    [_messageLabel removeFromSuperview];
    [self.view addSubview:self.pickerView];
    [self.view addSubview:self.leftPickerView];
    [self.view addSubview:self.label];
    self.isON = YES;
}

- (void)explain{
    WXSExplainViewController *explainVC = [[WXSExplainViewController alloc]init];
    [self.navigationController pushViewController:explainVC animated:YES];
}

#pragma mark - private method

- (NSString *)getCurrentTime{
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"HH:mm"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    currentTime = dateTime;
    _currentTimeLabel.text = currentTime;
    
    if (self.isON == NO) {
        return nil;
    }
    
    NSString *currentString = [formatter stringFromDate:currentDate];
    
    NSString *hour = [currentString substringToIndex:2];
    NSString *min = [currentString substringFromIndex:3];
    
    NSInteger h = [hour integerValue];
    NSInteger m = [min integerValue];
    
    NSInteger differTime;
    if ((self.H * 60 + self.M) - (h * 60 + m) > 0 ) {
        differTime = (self.H * 60 + self.M) - (h * 60 + m);
        
    }else{
        differTime = 24 * 60 - (h * 60 + m) + (self.H * 60 + self.M);
    }
    
    _Hours = differTime / 60;
    _Mins = differTime % 60;
    NSLog(@"HOURS %d",_Hours);
    NSLog(@"MINS %d",_Mins);
    
    
    _surTime = [NSString stringWithFormat:@"距离睡眠结束还剩%d时%d分",self.Hours,self.Mins];
    self.surplusTimeLabel.text = self.surTime;

    NSLog(@"%@",_surTime);

    return currentTime;
}

//- (void)_makeConstraints
//{
//    __weak typeof(self) myself = self;
//    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(0);
//        make.left.mas_equalTo(0);
//        make.right.mas_equalTo(0);
//        make.height.mas_equalTo(250 * kScreenHeightScale);
//    }];
//    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(myself.topView.mas_bottom);
//        make.left.mas_equalTo(0);
//        make.right.mas_equalTo(0);
//        make.bottom.mas_equalTo(0);
//    }];
//}

#pragma mark - CLZoomPickerViewDelegate CLZoomPickerViewDataSource

// CLZoomPickerView 代理，返回数据行数
- (NSInteger)pickerView:(CLZoomPickerView *)pickerView
{
    if (pickerView == self.pickerView)
        return self.timeArray.count;
    else
        return self.timeArr.count;
}

// CLZoomPickerView 代理，返回指定行显示的字符串
- (NSString *)pickerView:(CLZoomPickerView *)pickerView titleForRow:(NSUInteger)indexPath
{
    if (pickerView == self.pickerView)
        return self.timeArray[indexPath];
    else
    return self.timeArr[indexPath];
}

// CLZoomPickerView 代理，当前项改变后调用此方法
- (void)pickerView:(CLZoomPickerView *)pickerView changedIndex:(NSUInteger)indexPath
{
    if (pickerView == self.leftPickerView) {
        _hour = _timeArr[indexPath];
    }else if (pickerView == self.pickerView){
        _min = _timeArray[indexPath];
    }
    NSLog(@"aa%@",self.hour);
    NSLog(@"bb%@",self.min);
    
    _H = [self.hour integerValue];
    _M = [self.min integerValue];
    
    NSLog(@"%d",self.H);
    NSLog(@"%d",self.M);
    
//    NSString *dateString =  [NSString stringWithFormat:@"%@:%@",self.hour,self.min];
//    NSLog(@"data string %@",dateString);
//    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
//    formatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
//    [formatter setDateFormat:@"HH:mm"];
//    NSDate *date=[formatter dateFromString:dateString];
//    NSLog(@"date %@",date);
}

#pragma mark - lazy load

- (CLZoomPickerView *)pickerView{
    if (!_pickerView) {
        _pickerView = [[CLZoomPickerView alloc]initWithFrame:CGRectMake(kBaseWidth - 145, 100, 100, 180)];
        _pickerView.backgroundColor = [UIColor clearColor];
        self.pickerView.dataSource = self;
        self.pickerView.delegate = self;
        self.pickerView.topRowCount = 0;
        self.pickerView.bottomRowCount = 0;
        
        self.pickerView.selectedRow = 1;
        self.pickerView.rowHeight = 50;
        self.pickerView.selectedRowFont = [UIFont fontWithName:@"SFCompactDisplay-Ultralight" size:60];
        self.pickerView.textColor = [UIColor whiteColor];
        self.pickerView.unselectedRowScale = 0.4;
    }
    return _pickerView;;
}

- (CLZoomPickerView *)leftPickerView{
    if (!_leftPickerView) {
        _leftPickerView = [[CLZoomPickerView alloc]initWithFrame:CGRectMake(45, 100, 100, 180)];
        _leftPickerView.backgroundColor = [UIColor clearColor];
        self.leftPickerView.dataSource = self;
        self.leftPickerView.delegate = self;
        self.leftPickerView.topRowCount = 0;
        self.leftPickerView.bottomRowCount = 0;
        
        self.leftPickerView.selectedRow = 1;
        self.leftPickerView.rowHeight = 50;
        self.leftPickerView.selectedRowFont = [UIFont fontWithName:@"SFCompactDisplay-Ultralight" size:60];
        self.leftPickerView.textColor = [UIColor whiteColor];
        self.leftPickerView.unselectedRowScale = 0.4;
    }
    return _leftPickerView;;
}

- (NSArray *)timeArray{
    if (!_timeArray) {
        _timeArray = [NSArray arrayWithObjects:@"00",@"05",@"10",@"15",@"20",@"25",@"30",@"35",@"40",@"45",@"50",@"55",@"60", nil];
    }
    return _timeArray;
}

- (NSArray *)timeArr{
    if (!_timeArr) {
        _timeArr = [NSArray arrayWithObjects:@"00",@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23", nil];
    }
    return _timeArr;
}

- (UILabel *)currentTimeLabel{
    if (!_currentTimeLabel) {
        _currentTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(kBaseWidth/2 - 104, 66, 200, 60)];
        _currentTimeLabel.font = [UIFont fontWithName:@"SFCompactDisplay-Ultralight" size:60];
        _currentTimeLabel.text = currentTime;
        _currentTimeLabel.textColor = [UIColor whiteColor];
        _currentTimeLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _currentTimeLabel;
}

- (UILabel *)surplusTimeLabel{
    if (!_surplusTimeLabel) {
        _surplusTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(kBaseWidth/2 - 100, 130, 200, 30)];
        _surplusTimeLabel.font = [UIFont fontWithName:@"SFCompactDisplay-Ultralight" size:15];
        _surplusTimeLabel.textColor = [UIColor whiteColor];
        _surplusTimeLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _surplusTimeLabel;
}

- (UILabel *)label{
    if (!_label) {
        _label = [[UILabel alloc]initWithFrame:CGRectMake(kBaseWidth/2 - 5, 140, 10, 60)];
        _label.text = @":";
        _label.font = [UIFont fontWithName:@"SFCompactDisplay-Ultralight" size:60];
        _label.textColor = [UIColor whiteColor];
        _label.backgroundColor = [UIColor clearColor];
    }
    return _label;
}

- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(10, 80, kBaseWidth-20, 200)];
        _bgView.backgroundColor = [UIColor clearColor];
    }
    return _bgView;
}

- (UILabel *)messageLabel{
    if (!_messageLabel) {
        _messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(kBaseWidth/2-60, 160, 160, 40)];
        _messageLabel.text = @"闹钟未开启";
        _messageLabel.font = [UIFont fontWithName:@"SFCompactDisplay-Ultralight" size:25];
        _messageLabel.textColor = [UIColor whiteColor];
    }
    return _messageLabel;
}

@end
